package blog.controller;

import blog.entity.BatchResult;
import blog.entity.QueryJson;
import blog.entity.ResultJson;
import blog.pojo.ChapterCategory;
import blog.pojo.Config;
import blog.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/config")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @RequestMapping("batchEdit")
    public ResultJson batchEdit(@RequestBody QueryJson<Config> queryJson) throws Exception{
        int succ=0,fail=0;
        for(String name : queryJson.getOthers().keySet()){
            System.out.println(name +  "        " + queryJson.getOthers().get(name));
            if(configService.update(name, queryJson.getOthers().get(name))){
                succ++;
            } else {
                fail++;
            }
        }
        return ResultJson.isSuccess(BatchResult.SET(succ, fail));
    }

    @RequestMapping("findAll")
    public ResultJson findAll() throws Exception{
        return ResultJson.isSuccess(configService.selectAll());
    }
}
